<?php
ini_set('display_errors', 1);
error_reporting(-1);
echo PHP_VERSION, PHP_EOL;
// START@#2017-06-#

//exit();
// END@#2017-06-#

// START@#2017-06-22#

$str = "{\"branch_code\":\"650500\",\"branch_name\":\"\xe4\xba\x91\xe5\x8d\x97\xe5\x91\x88\xe8\xb4\xa1\xe5\x85\xac\xe5\x8f\xb8\",\"second_code\":\"049\",\"province\":\"\xe4\xba\x91\xe5\x8d\x97\xe7\x9c\x81\",\"city\":\"\xe6\x98\x86\xe6\x98\x8e\xe5\xb8\x82\",\"district\":\"\xe5\x91\x88\xe8\xb4\xa1\xe5\x8c\xba\",\"street\":\"\",\"multi_center\":1,\"trans_centers\":{\"650222\":{\"center_name\":\"\xe4\xba\x91\xe5\x8d\x97\xe6\x98\x86\xe6\x98\x8e\xe8\x88\xaa\xe7\xa9\xba\xe9\x83\xa8\",\"center_code\":\"650222\",\"mark\":\"\xe4\xba\x91\xe5\x8d\x97\xe5\x91\x88\xe8\xb4\xa1\",\"pack_name\":\"\xe6\x98\x86\xe6\x98\x8e\xe5\xb8\x82\xe5\x86\x85\xe5\x8c\x85\",\"pack_code\":\"851\",\"category\":\"\xe6\x8b\x86\",\"score\":2,\"candidateCount\":10,\"totalFound\":10}}}";
$ret = json_decode($str, true);

var_dump($ret);

exit();
// END@#2017-06-22#




// START@#2017-06-21#
$a = [];
function a(){return false;}
var_dump(current(a()));

exit();
// END@#2017-06-21#



// START@#2017-06-20#

echo (new DateTime('-6 months'))->format('Y-m-d');

exit;
$str =<<<SQL
-- 海南省三亚市天涯区天涯镇 [w7m8puc8kxd1]  8930
SELECT COUNT(*) FROM tbl_addresses_6168 WHERE amap_hash = 'w7m8puc8kxd1'


-- 广东省汕头市潮阳区海门镇[ws4gvyqnzske] [5151005559]  7708
SELECT COUNT(*) FROM tbl_addresses_5967 WHERE amap_hash = 'ws4gvyqnzske'

-- 上海市青浦区腾新路65号上海捷利跨境转运仓[wtw1u4db8c8n, wtw1u46tzf60]  73, 191
SELECT COUNT(*) FROM tbl_addresses_2 WHERE amap_hash = 'wtw1u4db8c8n'

-- 湖南省株洲市天元区泰山路街道
SELECT COUNT(*) FROM tbl_addresses_7164 WHERE amap_hash = 'wsbqq2z1vhsu'


-- 广东省深圳市宝安区沙井街道    8518
SELECT COUNT(*) FROM tbl_addresses_5947 WHERE amap_hash = 'ws0cmf7hr548'

-- 广东省东莞市长安镇   6752/297511
SELECT COUNT(*) FROM tbl_addresses_6131 WHERE amap_hash = 'ws0cv9kkqpzx'

-- 广东省東莞市虎門鎮赤崗富馬工業園點買貨運中轉站     11626
SELECT COUNT(*) FROM tbl_addresses_6131 WHERE amap_hash = 'ws0f59g5d11x'

-- 上海市奉贤区南桥镇   5770
SELECT COUNT(*) FROM tbl_addresses_2 WHERE amap_hash = 'wtqrgcg9dpgc'

-- 广东省佛山市南海区大沥镇   12935
SELECT COUNT(*) FROM tbl_addresses_5982 WHERE amap_hash = 'ws07myuq6ybm'


-- 广东省东莞市虎门镇   13198
SELECT COUNT(*) FROM tbl_addresses_6131 WHERE amap_hash = 'ws0cf9mf43qp'

-- 广东省佛山市南海区大沥镇   12935/231630
SELECT COUNT(*) FROM tbl_addresses_5982 WHERE amap_hash = 'ws07myuq6ybm'

-- 浙江省温州市鹿城区滨江街道 5645
SELECT COUNT(*) FROM tbl_addresses_843 WHERE amap_hash = 'wsvz66hb9jcv'
SELECT * FROM tbl_addresses_843 WHERE amap_hash = 'wsvz66hb9jcv' LIMIT 100

SQL;


exit();
// END@#2017-06-20#

// START@#2017-06-19#

// $recordUpdateDate = '2016-12-01';
// $date = '2017-03-05';
// $updateData = [];

// isset($recordUpdateDate) && $date !== $recordUpdateDate && strtotime($date) > strtotime($recordUpdateDate) && $updateData['update_date'] = $date;

// var_dump($updateData);
// exit;
$arr = [
    ['id' => 1, 'count' => 3],
    ['id' => 2, 'count' => 1],
    ['id' => 3, 'count' => 8],
    ['id' => 4, 'count' => 2],
    ['id' => 5, 'count' => 31],
];


// array_multisort(array_column($arr, 'count'), SORT_NUMERIC, SORT_DESC);
// uasort($arr, function($a, $b) {
//     return $b['count'] <=> $a['count'];
// });

var_dump($arr);
exit();
// END@#2017-06-19#

// START@#2017-06-15#

//replace directly or parse as array
$sql = "update tbl_scan_counterman set `identity`='业务员' where cm_phone=13939832231";
$patternWhere = '~where\s+(.+)(?:group|limit)?$~i';

preg_replace_callback($patternWhere, function ($match) {
    return preg_replace('~([a-zA-Z_\.]+\s*(?:[=><]|>=|<=|!=)\s*)(\d+)~', ' \1\'\2\' ', $match[1] . $match[2] . $match[3]) ?: $match[0];
}, $sql);

$where = [
    'a' => 1,
    'b' => 2,
    'C' => [1, 2, 3],
    'D IN' => [1, '11', 5, 6],
    'C > 5' => 5,
    'D in' => '["1", "2", 3]',
];
echo where($where);

function where($_where)
{
    $_table = 'tbl';
    if (empty($_where)) {
        return '';
    }

    // 拼接条件
    $condition = array();
    foreach ($_where as $key => $val) {

        // 去除字段前后空格 并且将多个空格转换成一个
        $key = trim(preg_replace('/\s+/', ' ', $key));

        // 将所有条件操作符转换成大写
        $key_arr = explode(' ', $key);
        if (isset($key_arr[1])) {
            $key = $key_arr[0] . ' ' . strtoupper($key_arr[1]);
        }

        // 匹配
        $part_one = preg_match('/\s[>][=]$|\s[<][=]$|\s[>]$|\s[<]$|\s[!][=]$|\s[<][>]$|\sLIKE$|\sIN$/', $key) ? $key : $key . ' =';

        // IN 查询
        if (preg_match('/\sIN$/', $key)) {
            if (is_array($val) && count($val)) {
                $in = array();
                foreach ($val as $k => $v) {
                    $in[$k] = "'" . addslashes($v) . "'";
                }
                $part_two = "(" . implode(',', $in) . ")";
            } else {
                $part_two = addslashes($val);
            }
        } else {
            // 强制转换成字符
            $val = (string) $val;

            // 处理is null或is not null场景
            $null_val = strtoupper(trim(preg_replace('/\s+/', ' ', $val)));
            if ($null_val == 'IS NULL' || $null_val == 'IS NOT NULL') {
                $part_one = str_replace('=', '', $part_one);
                $part_two = $null_val;
            } else {
                $part_two = "'" . addslashes($val) . "'";
            }
        }

        $part_one = preg_match('/[.]/', $part_one) ? $part_one : $_table . '.' . $part_one;
        $condition[] = $part_one . ' ' . $part_two;
    }

    $_where = array();
    return 'WHERE ' . implode(' AND ', $condition) . ' ';
}

exit();
//END@#2017-06-#
